package cn.kilo.domain.service.order.impl;

import cn.kilo.domain.model.order.entity.OrderStaffEntity;
import cn.kilo.domain.model.order.entity.StaffEntity;
import cn.kilo.domain.model.order.vo.OrderStatusVO;
import cn.kilo.domain.service.order.AbstractStaffStrategyChain;
import cn.kilo.domain.model.order.entity.OrderEntity;
import cn.kilo.domain.service.order.factory.DefaultFactory;
import cn.kilo.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author kilok
 * 保安：照看对应的物件
 */
@Slf4j
@Component("guard")
public class guardStrategy extends AbstractStaffStrategyChain {

    @Override
    public DefaultFactory.Role getRole() {
        return DefaultFactory.Role.GUARD;
    }

    @Override
    public Map<String, Integer> getLeftStaff(OrderEntity order) {
        return repository.getGuardStaff(order);
    }

    @Override
    public boolean beStaff(StaffEntity staffEntity) {
        boolean res=repository.checkGuard(staffEntity);
        if(res) {

            return false;
        }
        return repository.beGuard(staffEntity);
    }

    @Override
    public List<OrderStatusVO> doJob(OrderEntity order) {
        String guardId = order.getGuard();
        String staffName = repository.getStaffNameById(guardId);
        String cacheKey = Constants.RedisKey.ORDER_INFO + Constants.UNDERLINE + order.getOrderId();
        OrderStatusVO orderStatusVO = repository.getOrderStatusEntity(cacheKey);
        log.info("orderStatusVO:{}",orderStatusVO);
        String sorter1 = orderStatusVO.getStaffName();
        String sorter1Id=repository.getStaffIdByName(sorter1);
//        repository.deleteStaffKey(sorter1);
        repository.deleteStaffOrderInfo(sorter1Id,orderStatusVO);
        orderStatusVO.setStaffName(staffName);
        //设置结束时间
        orderStatusVO.setTimer(ChronoUnit.MINUTES.between(orderStatusVO.getNow(), LocalDateTime.now()));
        orderStatusVO.setNow(null);
        orderStatusVO.setStatus(DefaultFactory.Status.TAKE_2.getInfo());
        orderStatusVO.setStatusCode(DefaultFactory.Status.TAKE_2.getCode());
        String[] split = order.getEnd().split(Constants.UNDERLINE);
        Integer floor=Integer.valueOf(split[0]);
        String Floor=repository.getFloor(orderStatusVO.getRidgepole(),floor);
        String staffKey =
                Constants.RedisKey.STAFF_ORDER + Constants.UNDERLINE
                        + orderStatusVO.getRidgepole() + Constants.UNDERLINE + Floor;
        repository.updateOrder(orderStatusVO, staffKey, guardId);

        // TODO: 2024/5/18 添加对于orderStaff的更新操作（通过redis和定时任务实现）
        //添加对分拣员1对和该订单的sql更新操作
        orderStock.addOrderStaffStockQueue(OrderStaffEntity
                .builder()
                .orderId(order.getOrderId())
                .staffRole(DefaultFactory.Role.SORTER1.getCode())
                .staffName(sorter1)
                .end(orderStatusVO.getEnd())
                .time(orderStatusVO.getTimer())
                .distance(orderStatusVO.getDistance())
                .build());
        List<OrderStatusVO> list=new ArrayList<>();
        list.add(orderStatusVO);
        return list;
    }
}
